package com.example.webdemo.controller;

import com.example.webdemo.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;

/**
 * @Date 2021/6/22 11:29
 * @Created by wangle
 * @Description
 */
@Controller
public class indexcontroller {
//    @Autowired
//    StringRedisTemplate stringRedisTemplate;

//    来登录页
@GetMapping({"/","/login"})
    public  String logpage(){


        return "login";
    }

    @PostMapping("/login")
    public  String mainlogin(User user, HttpSession httpSession, Model model)
    {
        if(StringUtils.hasLength(user.getUserName()) && StringUtils.hasLength(user.getPassword())){
//把登陆成功的用户保存起来
            httpSession.setAttribute("loguser",user);
            return "redirect:/main.html";
        }else {

            model.addAttribute("msg","账号密码错误");
            return "login";

        }

        //登录成功重定向到main页面,重定向到main页面
//        return "main";


    }

//    去main页面
    @GetMapping("/main.html")
    public  String mainpage(HttpSession httpSession,Model model){


    //验证是否登录
//        Object user=httpSession.getAttribute("loguser");
//        if(user!=null){
//            return  "main";
//        }
//       else {
//
//            model.addAttribute("msg","账户为登录！");
//            return  "login";
//
//        }
//
//        ValueOperations<String ,String> operationsforvalue=stringRedisTemplate.opsForValue();
//        String ma=operationsforvalue.get("/main.html");
//        String dynamic_table=operationsforvalue.get("/dynamic_table");
//        model.addAttribute("maincount",ma);
//        model.addAttribute("dynamic_table",dynamic_table);


       return  "main";

    }



}
